from XSubmit.database import XSubmitDB


# 提供一些高级功能
class XSubmit(XSubmitDB):
    teacher = 'teacher'
    student = 'student'

    # 把这个post发给每一个target的收件箱box
    def post_broadcast(self, pid: int):
        post = self.post_get(pid)
        if 'targets' not in post['info']:
            post['info']['targets'] = []
        targets: list = post['info']['targets']
        try:
            if 'cid' in post:
                class_ = self.class_get(post['cid'])
                users = self.user_get_by_cid(class_['cid'])
                students = [user for user in users if user['type'] == self.student]
                students_uid = [student['uid'] for student in students]
                targets.extend(students_uid)
        except XSubmitDB.XSubmitDBExceptions.ObjectNotFound:
            pass
        if 'reply_to' in post:
            targets.append(post['reply_to'])
        for target in targets:
            self.box_add(target, post['pid'])

    # 看看有哪些人回复了邮件哪些人没有
    def post_count(self, pid: int) -> list:
        post = self.post_get(pid)
        if 'cid' not in post:
            raise XSubmit.XSubmitDBExceptions.ArgsError
        cid = post['cid']
        students = [student for student in self.user_get_by_cid(cid) if student['type'] == self.student]
        replies = self.post_get_by_reply(post['pid'])
        replied_uid = [p['uid'] for p in replies]
        replies_uid_post = {}
        for reply in replies:
            replies_uid_post[reply['uid']] = reply
        for i in range(len(students)):
            if 'appending' not in students[i]:
                students[i]['appending'] = {}
            if students[i]['uid'] in replied_uid:
                students[i]['appending']['replied'] = True
                students[i]['appending']['post'] = replies_uid_post[students[i]['uid']]
            else:
                students[i]['appending']['replied'] = False
        return students


def xsubmit_test_all():
    db = XSubmit()
    db.database_clear_all()
    db.database_init()

    # 新建一个学校
    school = db.school_new('哈尔滨工业大学（深圳）')
    print(school)
    # 建立一个班级
    class_ = db.class_new(school['sid'], '计科1班')
    print(class_)
    # 建立一个教师用户
    teacher = db.user_new('7老师', class_['cid'], '', 'teacher')
    print(teacher)
    # 建立一个学生用户
    student = db.user_new('提前退学', class_['cid'], '', 'student')
    print(student)
    # 教师发送一个post给学生
    post = db.post_new('测试POST', teacher['uid'], cid=class_['cid'])
    db.post_broadcast(post['pid'])
    print(post)
    # 学生查找收件箱
    boxes = db.box_find_by_uid(student['uid'])
    print(boxes)
    box = boxes[0]
    # 学生给老师写回信
    reply = db.post_new('Re: 测试POST', student['uid'], reply_to=box['pid'])
    db.post_broadcast(reply['pid'])
    # 学生设置已读一条收件箱
    db.box_read_one(box['bid'])
    print(db.box_find_by_uid(student['uid']))
    # 学生设置所有收件箱已读
    db.box_read_all_by_uid(student['uid'])
    print(db.box_find_by_uid(student['uid']))
    # 学生删除所有收件箱
    db.box_remove_by_uid(student['uid'])
    print(db.box_find_by_uid(student['uid']))
    # 老师查看收件箱
    print(db.box_find_by_uid(teacher['uid']))
    # 老师查看收件箱的邮件
    boxes = db.box_find_by_uid(teacher['uid'])
    box = boxes[0]
    reply = db.post_get(box['pid'])
    print(reply)
    # 老师看看有多少人回复了POST
    replies = db.post_get_by_reply(post['pid'])
    print(replies)
    # 老师看看统计情况
    count = db.post_count(post['pid'])
    print(count)


if __name__ == '__main__':
    xsubmit_test_all()
